/* Copyright (c) 2008 Google Inc.
*  Copyright (c) 2009 Pierre Henri Kuate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using org.opensocial.client;
using org.opensocial.data;

public static class DisplayMore
{
    public static void Run()
    {
        var c = new OpenSocialClient(Program.KnownGadgets["623061448914"]); // Orkut: http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml
        string ViewerId = "03067092798963641994"; // API DWH

        /*c = new OpenSocialClient(Program.KnownGadgets["http://opensocial-net-client.googlecode.com/svn/trunk/src/OpenSocial.Sample.Web/gadget.xml"]);
        ViewerId = "425505213"; // MySpace sample account/**/

        /*c = new OpenSocialClient(Program.KnownGadgets["hi5.com"]);
        ViewerId = "2"; // Hi5 random account/**/

        c.ViewerId = ViewerId;
        //c.RpcEndpoint = null; // RPC is preferred because it supports batch requests; so set it to null to use REST

        // Retrieve stuff
        //System.Console.Out.WriteLine(c.listMethods());
        //Output(c.fetchActivities());

        var viewerRequest = OpenSocialClient.newFetchPersonRequest(ViewerId)
            .setFields("@all");

        var friendsRequest = OpenSocialClient.newFetchFriendsRequest(ViewerId)
            //.setCount(1000) // TODO: Not supported by Hi5
            .setFields("@all");

        Response<Person> friends;

        if (c.UseRpc) // Use batch
        {
            var response = new OpenSocialBatch() // TODO: Make REST support this behind the scene... Users should not have to write two implementations
                .addRequest(viewerRequest, "viewerRequest")
                .addRequest(friendsRequest, "friendsRequest")
                .send(c);

            var viewer = response.getItemAsPerson("viewerRequest");
            friends = response.getItemAsPersonCollection("friendsRequest", c.UseRpc);
            friends.Insert(0, viewer);
        }
        else
        {
            var viewer = viewerRequest.requestObject(c);
            friends = friendsRequest.requestCollection(c);
            friends.Insert(0, viewer);
        }

        foreach (var friend in friends)
        {
            Output(friend);
            try
            {
                var albums = OpenSocialClient.newFetchAlbumsRequest(friend.Id)
                    //.setCount(1000) // TODO: Not supported by Hi5
                    .setFields("@all")
                    .requestCollection(c); // TODO: Orkut returns "unauthorized: You can only access @viewer's albums."
                foreach (var album in albums)
                {
                    Output(album);
                    var mediaItems = OpenSocialClient.newFetchMediaItemsRequest(album.OwnerId, "@self", album.Id)
                        //.setCount(1000) // TODO: Not supported by Hi5
                        .setFields("@all")
                        .requestCollection(c);
                    Output(mediaItems);
                    System.Console.Out.WriteLine();
                }
            }
            catch (System.Exception ex)
            {
                System.Console.Out.WriteLine("Albums not accessible:\n" + ex.Message);
            }
            System.Console.Out.WriteLine("--------------------\n--------------------\n");
        }

        System.Console.Out.WriteLine("--------------------");
        System.Console.Out.WriteLine("Press a key to exit!");
        System.Console.ReadKey();
    }

    private static void Output<T>(Response<T> entities)
        where T : OpenSocialObject
    {
        System.Console.Out.WriteLine("--------------------");

        System.Console.Out.WriteLine(entities.Count + " " + typeof(T).Name + "(s): startIndex=" + entities.startIndex + ", totalResults=" + entities.totalResults);

        foreach (var e in entities)
            System.Console.Out.WriteLine("- " + e);
    }

    private static void Output<T>(T entity)
    {
        System.Console.Out.WriteLine("--------------------");

        System.Console.Out.WriteLine(entity);
    }
}